// 导入数据库操作模块,一般叫做db
const db = require('../db')
// 查询表格的方法
exports.reqTableList = async (req, res) => {
  const sql_Jincheng = `select * from var_table_jinchengzhuangtai_fen where isdelete=0 order by id asc`
  const sql_fenye = 'select * from var_table_main where isdelete=0 order by id limit ?,?'
  const sqlTotal = 'select count(*) as total from var_table_main' //as更换名称
  // 前端不传参数默认 返回 0 10
  // 页码
  const page = +req.query.page || 1
  // 条数
  const pagesize = +req.query.pagesize || 1
  // 总条数
  const params = [(Number(page) - 1) * Number(pagesize), Number(pagesize)]
  // 主
  Promise.all([p(sql_fenye, params), p(sql_Jincheng), p(sqlTotal)])
    .then(([result_fenye, result_2, count]) => {
      // console.log('🚚...  信息', count)
      // console.log('🚚...  信息', result_fenye)
      // console.log('🚚...  信息', result_2)
      let total = count[0]['total'] //查询表中的数量
      let arr = {
        code: 0,
        message: '获取商品分类成功',
        data: {
          list: result_fenye,
          page,
          pagesize,
          total: total
        }
      }
      arr.data.list.map((item, i) => {
        item.listStatus = result_2
      })
      res.send(arr)
    })
    .catch(err => {
      console.log('err', err.message)
      res.json({
        code: 1,
        data:{
          list:[]
        },
        message: '查询失败'
      })
    })
}
function p(sql, params) {
  return new Promise((resolve, reject) => {
    db.query(sql, params, (err, result) => {
      if (err) return reject(err)
      resolve(result)
    })
  })
}
